/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lightframework.jamwiki.servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jamwiki.model.VirtualWiki;
import org.jamwiki.servlets.AdminVirtualWikiServlet;
import org.jamwiki.servlets.ServletUtil;
import org.jamwiki.servlets.WikiPageInfo;
import org.lightframework.jamwiki.utils.WikiDatabaseEx;
import org.springframework.web.servlet.ModelAndView;

/**
 * extend the jamwiki servlet {@link AdminVirtualWikiServlet} to setup default pages when wiki created.
 * 
 * @author fenghm(live.fenghm@gmail.com)
 */
public class AdminVirtualWikiServletEx extends AdminVirtualWikiServlet {

	@Override
    protected ModelAndView handleJAMWikiRequest(HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo)
            throws Exception {

	    ModelAndView mv = super.handleJAMWikiRequest(request, response, next, pageInfo);
	    
	    //if create or update a wiki
	    if("virtualwiki".equals(request.getParameter("function"))){
	    	boolean createNew = StringUtils.isBlank(request.getParameter("virtualWikiId"));
	    	if(createNew){
	    		VirtualWiki wiki = (VirtualWiki)mv.getModel().get("selected");
	    		WikiDatabaseEx.setupDefaultPages(request.getLocale(), wiki.getName(), ServletUtil.currentWikiUser());
	    	}
	    }
	    
	    return mv;
    }
}
